package mhys.View.Teacher;

import mhys.Dao.ClassesDao;
import mhys.Dao.StuDao;
import mhys.Entity.Classes;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class ClassJpanel extends JPanel {
    DefaultTableModel model;
    JTable jTable;
    ClassesDao classesDao = new ClassesDao();
    StuDao stuDao = new StuDao();
    public ClassJpanel(){
        this.setBackground(Color.WHITE);
        this.setLayout(new BorderLayout(10,10));
        JPanel jPanel = new JPanel();
        jPanel.setLayout(null);

        JLabel classJLabel = new JLabel("班级：");
        classJLabel.setBounds(10,60,80,25);
        final JComboBox<String> classBox = new JComboBox<String>();
        List<Classes> classes = classesDao.getClasses();
        for (Classes classes1 : classes) {
            classBox.addItem(classes1.getC_name());
        }
        classBox.setBounds(90,60,150,25);


        JLabel marjor = new JLabel("专业：");
        marjor.setBounds(10,100,80,25);
        final JComboBox<String> objBox = new JComboBox<String>();
        //去重
        Set<String> set = new HashSet<String>();
        List<Classes> classes2 = classesDao.getMajor();
        for (Classes c: classes2) {
            String s = c.getMajor();
            if (set.add(s)) {
                objBox.addItem(s);
            }
        }
        objBox.setBounds(90,100,150,25);

        JLabel teaJLabel = new JLabel("班主任：");
        teaJLabel.setBounds(10, 140, 80, 25);
        final JTextField teaTextField = new JTextField();
        teaTextField.setBounds(90, 140, 150, 25);

        JButton addButton = new JButton("添加");
        addButton.setBounds(100, 180, 80, 25);

        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String classes = (String)classBox.getSelectedItem();
                String marjor = (String)objBox.getSelectedItem();
                String tea = teaTextField.getText();
                if (classes.isEmpty() || marjor.isEmpty() || tea.isEmpty()){
                    JOptionPane.showMessageDialog(null, "不能为空");
                }else {
                    Classes classes1 = new Classes(null,classes,marjor,tea);
                    int addTea = classesDao.addClass(classes1);
                    if (addTea == 1){
                        List<Classes> classesList = classesDao.getClasses();
                        JOptionPane.showMessageDialog(null, "添加成功！");
                        classBox.setSelectedItem("");
                        teaTextField.setText("");
                        objBox.setSelectedItem("");
                    }
                }

            }
        });


        jPanel.add(objBox);
        jPanel.add(marjor);
        jPanel.add(classJLabel);
        jPanel.add(classBox);
        jPanel.add(teaJLabel);
        jPanel.add(teaTextField);
        jPanel.add(addButton);
        jPanel.setBackground(Color.white);
        this.add(jPanel,BorderLayout.CENTER);
    }
}
